/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package register.ucm;
import common.ReConvertor;
import register.model.InputValueModel;
import register.model.ButtonModel;
import register.view.RegisterPanel;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
 *
 * @author Vitamin
 */
public class GenerateID {

    private InputValueModel value;
    private ButtonModel button;
    private String id;
    private GenerateCollegeCode college;
    private GenerateCourseCode course;
    private GenerateDegreeCode degree;
    private GenerateCountCode count;
    private GenerateDept dept;
    private String year;
    private JTextField rid;
    private Date thisYear;
    private String sql;

    public GenerateID()
    {
        this.init();
    }

    public void init()
    {
        value=RegisterPanel.getInstance().getInputValueModel();
        button=RegisterPanel.getInstance().getButtonModel();
        rid=button.getRID();
        college=new GenerateCollegeCode(value);
        course=new GenerateCourseCode(value);
        degree=new GenerateDegreeCode(value);
        count=new GenerateCountCode(value);
        dept=new GenerateDept(value);
        thisYear=new Date();
        SimpleDateFormat kiper=new SimpleDateFormat("yy"); // Define the format of year which is the first 4 codes of personal ID
        year=kiper.format(thisYear);
    }

    public void generateID()
    {
        if(value.getGroup().equals("Student"))
        {
            sql="SELECT student_id FROM student WHERE major_course='"+ value.getMajorCourse()+"'";
//            System.out.println("SQL as "+sql);
            id=year + degree.generateDegree() + college.generateCollege() + course.generateCourse() + count.getCount(sql);
            rid.setText(id);
            value.setId(id);
        }
        else if(value.getGroup().equals("Teacher"))
        {
            sql="SELECT teacher_id FROM teacher WHERE college='"+ ReConvertor.getInstance().teacherGroup(value.getCollege())+"'";
//            System.out.println("SQL as "+sql);
            id=year + "8" + college.generateCollege() + course.generateCourse() + count.getCount(sql);
            rid.setText(id);
            value.setId(id);
        }
        else if(value.getGroup().equals("Staff"))
        {
            sql="SELECT staff_id FROM staff WHERE groups='" +ReConvertor.getInstance().staffGroup(value.getDepartment())+"'";
//            System.out.println("SQL as "+sql);
            id=year + "9" + dept.generateDept() + course.generateCourse() + count.getCount(sql);
            rid.setText(id);
            value.setId(id);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Choose a group to complete register!");
        }
    }


}
